// packageA/pages/seckill/checkout.js
const app = getApp();
let a, t, i, e, n, s, d, o, r, p, c, h,
f = new Array(),
l = {
  consignee: 1,
  shipping_id: [],
  msg: ""
},
u = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(a) {
  return typeof a;
} : function(a) {
  return a && "function" == typeof Symbol && a.constructor === Symbol && a !== Symbol.prototype ? "symbol" : typeof a;
};
Page({

  /**
   * 页面的初始数据
   */
  data: {
    index:0,
    isShow: false,
    shipping_id: 0,
    shopLists: "",
    resShipingId: [],
    payfee: [],
    addresss_link: "../../../pages/address/index?from=seckill",
    flow_type:[],
    is_first_action: !0,
  },
  // 右侧导航
  toggle: function () {
    let that = this;
    that.setData({
      isShow: !that.data.isShow
    })
  },

  //提交订单
  submitOrder: function() {
    let t = this;
    a = wx.getStorageSync("token");
    if (1 == t.data.is_first_action) {
      if (t.setData({
          is_first_action: !1
        }), 1 == t.data.payfee_error) return void wx.showToast({
        title: "地区不支持配送，无法提交",
        image: "../../../images/failure.png",
        duration: 2e3
      });
      if ("" == l.consignee || void 0 == l.consignee) return void _.shwomessage("没有收货地址");
      r = {
        inv_payee: "",
        inv_content: ""
      };
      if (0 == i) {
        if ("单位" == e) r = {
          invoice_type: i,
          inv_payee: o,
          tax_id: n,
          inv_content: s
        };
        if ("个人" == e) r = {
          invoice_type: i,
          inv_payee: e,
          inv_content: s
        };
      }
      if (1 == i) var r = {
        invoice_type: i,
        vat_id: t.data.invoice_id,
        inv_payee: "",
        inv_content: "",
        tax_id: ""
      };
      wx.request({
        url: app.apiUrl("flow/down"),
        method: "post",
        data: {
          consignee: l.consignee,
          shipping: this.data.resShipingId,
          postdata: r,
          uc_id: d || "0",
          bs_id: t.data.cont_data.bs_id,
          flow_type: t.data.cont_data.flow_type,
          team_id: t.data.cont_data.team_id,
          t_id: t.data.cont_data.t_id,
          is_immediately: t.data.is_immediately
        },
        header: {
          "Content-Type": "application/json",
          "X-ECTouch-Authorization": a
        },
        success: function(a) {
          console.log('a',a)
          let t = a.data.data;
          a.data.code > 0 ? wx.showToast({
            title: a.data.data,
            image: "../../../images/failure.png",
            duration: 2e3
          }) : "" != t && wx.redirectTo({
            url: "../../../pages/flow/done?id=" + t
          });
        }
      });
    }
  },

  getShopName: function(a) {
    let t = this,
      i = [],
      e = [];
    for (let n in this.data.shopLists.list) ! function(a) {
      let n = [],
        s = [],
        d = {
          option: [],
          id: 0
        },
        o = t.data.shopLists.list[a];
      "" != o.shop_info && (o.shop_info.forEach(function(a) {
        n.push(a.shipping_name), s.push(a.shipping_id);
      }), 
      d.option = n, 
      i.push(d), 
      e.push(s), 
      l.shipping_id = s[0], 
      t.data.resShipingId.push({
        ru_id: o.shop_info[0].ru_id,
        shipping_id: o.shop_info[0].shipping_id
      }));
    }(n);
    this.setData({
      shippingName: i,
      shpppingId: e
    });
  },

  shippingChange: function(t) {
    let i = this;
    if ("object" == (void 0 === t ? "undefined" : u(t)) && 2 == t.length) var e = t[0],
      n = t[1];
    else var e = t.currentTarget.dataset.index,
      n = t.detail.value;
    i.data.shippingName[e].id = n, l.shipping_id = this.data.shpppingId[e][n];
    // this.data.shopLists.list[e].shop_list[0].ru_id;
    let s = 0;
    this.data.resShipingId[e].shipping_id = this.data.shopLists.list[e].shop_info[n].shipping_id,
    a = wx.getStorageSync("token"),
      this.setData({
        shippingName: this.data.shippingName,
        resShipingId: this.data.resShipingId
      }), 
      wx.request({
        url: app.apiUrl("flow/shipping"),
        data: {
          address: l.consignee,
          id: this.data.resShipingId[e].shipping_id,
          ru_id: this.data.resShipingId[e].ru_id,
          flow_type: p || "0",
          uc_id: i.data.cou_id ? i.data.cou_id : 0
        },
        method: "POST",
        header: {
          "Content-Type": "application/json",
          "X-ECTouch-Authorization": a
        },
        success: function(a) {
          f[i.data.resShipingId[e].ru_id] = {
              shipping_id: i.data.resShipingId[e].shipping_id,
              shipping_fee: parseFloat(a.data.data.fee)
            },
            i.data.payfee.hasOwnProperty(e), 
            a.data.data.fee ? i.data.payfee.splice(e, 1, a.data.data.fee) : i.data.payfee.splice(e, 1, a.data.data.massage),
            1 == a.data.data.error && wx.showToast({
              title: a.data.data.massage,
              image: "../../../images/failure.png",
              duration: 2e3
            }), 
            i.data.payfee.forEach(function(a) {
              isNaN(a) || (s += parseFloat(a));
            }), 
            i.setData({
              payfee: i.data.payfee,
              payfee_total: s,
              total: parseFloat(i.data.shopLists.order_total) + s - parseFloat(i.data.cou_money ? i.data.cou_money : 0),
              payfee_error: a.data.data.error
            });
        }
      });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    let shop_name = wx.getStorageSync("shop_name");
    that.setData({
      shop_name: shop_name,
      flow_type: options.flow_type
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this;
    let a = wx.getStorageSync("token");
    wx.request({
      url: app.apiUrl("seckillflow"),
      data: {
        flow_type: that.data.flow_type
      },
      method: "post",
      header: {
        "Content-Type": "application/json",
        "X-ECTouch-Authorization": a
      },
      success: function(res) {
        console.log('seckillflow',res.data.data)
        if (res.data.data == '' || res.data.data == undefined) {
          wx.navigateTo({
            url: that.data.addresss_link
          })
        }else{
          that.setData({
            addresss: res.data.data.default_address,
            shopLists:res.data.data.cart_goods_list,
            coupons_list:res.data.data.coupons_list,
            cont_data: res.data.data,
            hidden: true
          })
          that.getShopName();
          for (let i in that.data.shippingName) that.shippingChange(new Array(i, that.data.shippingName[i].id));
        }
      }
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})